package com.fh.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fh.common.exception.Assert;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.system.entity.SysDict;
import com.fh.modules.system.entity.SysDictItem;
import com.fh.modules.system.mapper.SysDictMapper;
import com.fh.modules.system.model.SysDictTree;
import com.fh.modules.system.service.ISysDictItemService;
import com.fh.modules.system.service.ISysDictService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Qiu,Junda
 * @date 2019年4月3日
 * @description 字典表 服务实现类
 */
@Service
@Transactional
@Slf4j
public class SysDictServiceImpl extends AbstractServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

	@Autowired
	private SysDictMapper sysDictMapper;
	@Autowired
	private ISysDictItemService sysDictItemService;
	@Override
	public List<Map<String, String>> queryDictItemsByCode(String code) {
		return sysDictMapper.queryDictItemsByCode(code);
	}
	
	@Override
	public String queryDictTextByKey(String code, String key) {
		return sysDictMapper.queryDictTextByKey(code, key);
	}

	@Override
	public List<SysDictTree> queryDictByParentId(String parentId) {
		List<SysDictTree> treeList = new ArrayList<>();
		LambdaQueryWrapper<SysDict> query = new LambdaQueryWrapper<>();
		parentId=(parentId==null)?"":parentId.trim();
		query.eq(true, SysDict::getParentId,parentId);
		query.eq(true, SysDict::getDelFlag, "0");
		query.orderByDesc(true, SysDict::getCreateTime);
		List<SysDict> list = this.list(query);
		for (SysDict node : list) {
			SysDictTree sysDictTree=new SysDictTree(node);
			if(node.getDictType()==2) {
				sysDictTree.getChildren().addAll(queryDictByParentId(node.getId()));
			}
			treeList.add(sysDictTree);
		}
		return treeList;
	}

	@Override
	public void deleteDict(String id) {
		SysDict sysDict = this.getById(id);
		Assert.isNull(sysDict, "删除失败：未找到对应实体");
		sysDictItemService.deleteDictItemsByDictId(id);
		sysDict.setDelFlag(1);
		this.updateById(sysDict);
		List<String> children=sysDictMapper.queryAllChildren(id);
		for (String childId : children) {
			this.deleteDict(childId);
		}
	}

	@Override
	public void deleteDictByIds(String ids) {
		String[] id=ids.split(",");
		for(int i=0;i<id.length;i++) {
			this.deleteDict(id[i]);
		}
	}

	@Override
	public List<Map<String, Object>> queryTableDictItemsByCode(String table, String text, String code) {
		log.info("无缓存dictTableList的时候调用这里！");
		return sysDictMapper.queryTableDictItemsByCode(table,text,code);
	}

	@Override
	public String queryTableDictTextByKey(String table, String text, String code, String key) {
		log.info("无缓存dictTable的时候调用这里！");
		return sysDictMapper.queryTableDictTextByKey(table,text,code,key);
	}

	@Override
	public boolean deleteByDictId(SysDict sysDict) {
		sysDict.setDelFlag(2);
        this.updateById(sysDict);
        // 删除关联表中的数据
        String deleteId = sysDict.getId();
        LambdaQueryWrapper<SysDictItem> dictItemQuery = new LambdaQueryWrapper<SysDictItem>();
        dictItemQuery.eq(SysDictItem::getDictId, deleteId);
        boolean ok = sysDictItemService.remove(dictItemQuery);
        return ok;
	}

	@Override
	public void saveMain(SysDict sysDict, List<SysDictItem> sysDictItemList) {
//		sysDictMapper.insert(sysDict);
//        if (sysDictItemList != null) {
//            for (SysDictItem entity : sysDictItemList) {
//                entity.setDictId(sysDict.getId());
//                sysDictItemMapper.insert(entity);
//            }
//        }
		
	}

}
